<?php
require('config.php'); // define yahoo APP_ID constant

/******************************************************************************/
function getSites() {
    $numItems = 100;
    $start = 1;
    
    //$query = urlencode('inurl:?params=submit/form/');
    $query = urlencode('inurl:/submit/form "URL to thumbnail"');

    $sites = array();

    do {
        $request = "http://search.yahooapis.com/WebSearchService/V1/webSearch?appid=" . APP_ID . "&query=$query&start=$start&results=$numItems&language=en&output=php";
        echo "request: $request\n";
    
        // Make the request
        $results = file_get_contents($request);
        $results = unserialize($results);

        $sites = array_merge($sites, $results['ResultSet']['Result']); 

        $start = $start + $numItems; 

    } while ($results['ResultSet']['totalResultsReturned'] == $numItems); 

    return $sites;
}
/******************************************************************************/
function submitGSS($url, $game) {

    $game = array_merge($game, array('params' => 'submit/do', 'submit' => 'Submit'));
    $fields = http_build_query($game);
    
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $result = curl_exec($curl);
    curl_close($curl);
  
    return $result; 
}
/******************************************************************************/

$sites = getSites();

/* put your own game here
$game = array('name' =>  'Flip Flop',
    'desc' => 'Try to turn on all the yellow lights.',
    'cat' => 'Puzzle',
    'swf' => 'http://games.mochiads.com/c/g/flip-flop_v1/flipflop.swf',
    'thumb' => 'https://sandbox.mochiads.com/c/g/flip-flop_v1/_thumb_100x100.jpg',
    'authorname' => 'Emmett Henderson',
    'authorsite' => 'http://www.cheesygames.com');
/*

foreach ($sites as $site) {
    $url = $site['DisplayUrl'];
    $url = substr($url, 0, strpos($url, '/'));
    echo "Posting: $url\n";  

    $result = submitGSS($url, $game);

    $log = str_repeat('=', 80) . "\n";
    $log .= "Posted: $url - " . $site['DisplayUrl'] . "\n";
    $log .= "Result:\n$result\n";

    file_put_contents('submitgss.log', $log, FILE_APPEND);

}

?>

